package com.dreamthen.servlets;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author zengms
 * @Date 2023-10-17 11:10:41
 **/
public class HelloServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("***doGet***");
        //通常会在doGet和doPost做一些处理
        //判断是什么请求，根据不同请求，做不同的分发处理，比如login()、register()
        //获取参数，并将参数封装到一个实体类中->请求数据库->响应
        String action = req.getParameter("action");
        if (null != action && action.equals("login")) {
            login(req, resp);
        }

    }

    private void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取参数，并将参数封装到一个实体类中->请求数据库->响应
        String userName = req.getParameter("userName");
        System.out.println("userNam=" + userName);
        //登录业务处理
        //请求数据库
        //响应，跳转到首页
        resp.sendRedirect("index.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("***doPost****");
        super.doPost(req, resp);
    }
}
